AddCSLuaFile("cl_flag.lua")

module("Flag", package.seeall )

DEBUG = false

function GetSkin( team )
	if team == Team.BLUE then
		return 0
	end
	return 2
end

function ResetTime()
	return CurTime() + 5 * 60
end

function SpawnAll()

	if DEBUG == true then
		print("Spawning all flags!")
	end

	for _, prop in pairs( ents.FindByClass("prop_flag") ) do
	
		local ent = ents.Create("sf_flag")
		ent:SetOriginProp( prop )
		ent:Reset()
		ent:Spawn()	
	
	end

end

function RemoveAll()

	if DEBUG == true then
		print("Removing all flags!")
	end
	
	for _, ent in pairs( ents.FindByClass("sf_flag") ) do
		
		SafeRemoveEntity( ent )
		
	end
	
	for _, ply in pairs( player.GetAll() ) do
		ply._NCPickedFlag = nil
	end
	
end

function Get( ply )
	return ply._NCPickedFlag
end

function Set( ply, ent )
	ply._NCPickedFlag = ent
	
	if DEBUG then
		print("Flag: Set: ", ply, ent  )
	end
	
	if ValidEntity( ent ) then
		ent:Pickup( ply )
		ent:StopResetTimer()
	end
end

function Drop( ply )
	local flag = Get( ply )
	
	if DEBUG then
		print("Flag: Drop: ", ply, flag  )
	end
	
	if !ValidEntity( flag ) then
		return
	end
	
	ply._NCPickedFlag = nil
	
	flag:Drop()
	flag:StartResetTimer( ResetTime() )
	
	hook.Call("FlagDrop", GAMEMODE, ply, flag )

	umsg.Start("SFFlag", nil )
		umsg.Char(3)
		umsg.Entity(ply)
		umsg.Entity(flag)		
	umsg.End()
	
end

function Pickup( ply, flag )
	
	if DEBUG then
		print("Flag: Begin: Pickup:", ply, flag, ply:Team(), flag:Team()  )
	end
	
	if ply:Team() == flag:Team() then
		//Check if the flag is unfrozen and return it
		if !ValidEntity( flag:GetParent() ) && flag:HasTimer() then
			Reset( flag )
		end
	
		return
	end

	if ValidEntity( Get( ply ) ) then
		return //Player already has a flag
	end
	
	local parent = flag:GetParent()
	
	if ValidPlayer( parent ) then
		return //Flag is already picked up
	end
	
	if DEBUG then
		print("Flag: Pickup:", ply, flag )
	end
	
	Set( ply, flag )
	
	hook.Call("FlagPicked", GAMEMODE, ply, flag )
	
	umsg.Start("SFFlag", nil )
		umsg.Char(2)
		umsg.Entity(ply)
		umsg.Entity(flag)		
	umsg.End()
	
end

function Reset( flag )

	local parent = flag:GetParent()
	
	if DEBUG then
		print("Flag: Reset:", flag, parent )
	end
	
	if ValidPlayer( parent ) then
		parent._NCPickedFlag = nil
	end
	
	flag:Reset()
	
	hook.Call("FlagReset", GAMEMODE, parent, flag )
	
	umsg.Start("SFFlag", nil )
		umsg.Char(1)
		umsg.Entity(parent)
		umsg.Entity(flag)		
	umsg.End()
	
end

function TouchFlagZone( ply, zone )
	
	local flag = Get( ply )
	local TeamId = ply:Team()
	
	if ValidEntity( flag ) then
	
		if zone:Team() == TeamId then
		
			team.SetScore( TeamId, team.GetScore( TeamId ) + 1 )
			Reset( flag )
			
			hook.Call("FlagCaptured", GAMEMODE, ply, flag, zone )
			
			umsg.Start("SFFlag", nil )
				umsg.Char(0)
				umsg.Entity(ply)
				umsg.Entity(flag)		
			umsg.End()
			
		end
	
	end
	
end


hook.Add("StateChange", "SpawnFlag", function( state )

	if state == Game.BUILD then
		RemoveAll()
	elseif state == Game.FIGHT then
		SpawnAll()
	end
	
end )

hook.Add("PlayerDeath", "ResetFlag", function( ply )
	
	local flag = Get( ply )
	
	if DEBUG then
		print("Flag: Player death: ", ply, flag )
	end
	
	if ValidEntity( flag ) then
		Drop( ply )
	end	

end )